package com.qf.shop.order.service.impl;

import com.qf.shop.common.exception.BusinessException;
import com.qf.shop.common.vo.*;
import com.qf.shop.order.entity.OrderDetailEntity;
import com.qf.shop.order.entity.OrderEntity;
import com.qf.shop.order.feign.api.AddressService;
import com.qf.shop.order.feign.api.CarService;
import com.qf.shop.order.feign.api.StockService;
import com.qf.shop.order.interceptor.LoginInterceptor;
import com.qf.shop.order.interceptor.OrderDynamicDatasourceInterceptor;
import com.qf.shop.order.mapper.OrderMapper;
import com.qf.shop.order.service.OrderService;
import com.qf.shop.order.utils.OrderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Service
@Slf4j
public class OrderServiceImpl2 implements OrderService {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private OrderUtils orderUtils;

    @Autowired
    private AddressService addressService;

    @Autowired
    private CarService carService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDynamicDatasourceInterceptor orderDynamicDatasourceInterceptor;

    @Autowired
    private StockService stockService;

    /**
     * 创建订单
     *
     * @param orderVo
     * @return
     */
    @Override
    public String create(OrderVo orderVo) {


//        // 1.启动一个任务查询用户的地址
//        CompletableFuture.supplyAsync(() -> {
//            return null;
//        }, ShopExcutors.getExecutor());
//
//        // 2.启动一个任务查询用户的购车


        // 1.先获取登录用户
        UserVo userVo = loginInterceptor.get();

        // 2.创建订单ID(yyyMMdd+用户ID后四位+4位随机数+流水号(唯一))
        String orderId = orderUtils.createOrderId(userVo.getId());

        // 3、根据地址Id查询地址信息
        AddressVo addressVo = addressService.getAddressVoById(orderVo.getAid());

        // 4.查询用户的购物车
        String selectGid = orderVo.getSelectGid(); // 1_12,3_14,1_13

        // 5.查询用户的购车
        UserCarVo userCarVo = carService.getUserCar(orderVo.getSelectGid());

        // 6.封装订单数据
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(orderId);
        orderEntity.setUid(Integer.parseInt(userVo.getId()));
        orderEntity.setAddress(addressVo.getAddress());
        orderEntity.setPhone(addressVo.getPhone());
        orderEntity.setName(addressVo.getName());
        orderEntity.setStatus(1); // 1
        orderEntity.setTotalPrice(userCarVo.getTotalPrice());
        orderEntity.setCreateTime(new Date());
        orderEntity.setPayType(orderVo.getPayType());

        log.debug("orderEntity:{}", orderEntity);

        // 扣减库存
        List<StockVo> stockVoList = new ArrayList();
        for (StoreCarVo storeCarVo : userCarVo.getStoreCarVoList()) {
            for (CarVo carVo : storeCarVo.getCarVoList()) {
                StockVo stockVo = new StockVo();
                stockVo.setGid(carVo.getGid());
                stockVo.setCount(carVo.getCount());
                stockVoList.add(stockVo);
            }
        }

        Boolean stockFlag = stockService.lockStock(stockVoList);
        if (!stockFlag) {
            throw new BusinessException("扣减库存失败", 3001);
        }


        // 7.创建订单数据(先确定数据源，再设置数据表的编号)
        Integer tabIndex = orderDynamicDatasourceInterceptor.get();
        int order = orderMapper.createOrder(orderEntity, tabIndex);
        log.debug("order:{}", order);

        // 8、插入订单详情
        List<OrderDetailEntity> odList = new ArrayList<>();

        for (StoreCarVo storeCarVo : userCarVo.getStoreCarVoList()) {
            for (CarVo carVo : storeCarVo.getCarVoList()) {

                // 把用户购买的数据封装到订单闲情中
                OrderDetailEntity odEntity = new OrderDetailEntity();
                odEntity.setCount(carVo.getCount());
                odEntity.setGdesc(carVo.getGoodsVo().getGdesc());
                odEntity.setGid(carVo.getGid());
                odEntity.setGprice(carVo.getGoodsVo().getGprice());
                odEntity.setGname(carVo.getGoodsVo().getGname());
                odEntity.setGurl(carVo.getGoodsVo().getGoodsPngList().get(0).getUrl());
                odEntity.setOid(orderId);
                odEntity.setSid(storeCarVo.getStoreVo().getId()); //
                odEntity.setSubTotal(carVo.getSubTotal());

                odList.add(odEntity);
            }
        }

        // 批量插入订单详情
        int odCount = orderMapper.batchInsertOrderDetail(odList, tabIndex);
        log.debug("odCount:{}", odCount);

        return orderId;
    }

    @Override
    public OrderEntity getOrderById(String orderId) {
        return null;
    }

    @Override
    public void updateOrderStatus(String orderId, Integer status) {

    }

    @Override
    public List<StockVo> getStockVoListOrderId(String orderId) {
        return null;
    }
}
